#include <unordered_map>
#include <vector>
#include <pair>
using namespace std;

class Solution {
public:
    class myCompare{
    public:
        bool operator()(const pair<int, int> &lhs, const pair<int, int> &rhs){
            return lhs.second > rhs.second;
        }
    };

    vector<int> topKFrequent(vector<int>& nums, int k) {
        unordered_map<int, int> umap;
        for (int i=0; i<nums.size(); i++){
            umap[nums[i]]++;
        }

        priority_queue<pair<int, int>, vector<pair<int, int>>, myCompare> pri_que;
        for(auto it = umap.begin(); it != umap.end(); it++){
            pri_que.push(*it);
            if(pri_que.size() > k){
                pri_que.pop();
            }
        }

        vector<int> res(k);
        for(int i=k-1; i>=0; i--){
            res[i] = pri_que.top().first;
            pri_que.pop();
        }
        return res;
    }
};